package club.banyuan;

import org.junit.jupiter.api.Test;

import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertTrue;

class MyArrayListTest {
  private final int SIZE = 20;
  private final int BOUND = 100;
  private final Random random = new Random();

  private MyArrayList<Integer> genList() {
    MyArrayList<Integer> list = new MyArrayList<>();
    for (int i = 0; i < SIZE; i++) {
      list.addAtTail(random.nextInt(BOUND));
    }
    return list;
  }

  private void verifySortedList(MyArrayList<Integer> list) {
    Integer pre = Integer.MIN_VALUE;
    for (int i = 0; i < SIZE; i++) {
      Integer curr = list.get(i);
      assertTrue(pre <= curr);
      pre = curr;
    }
  }

  @Test
  void insertionSort() {
    var list = genList();
    list.insertionSort();
    verifySortedList(list);
  }

  @Test
  void selectionSort() {
    var list = genList();
    list.selectionSort();
    verifySortedList(list);
  }

  @Test
  void bubbleSort() {
    var list = genList();
    list.bubbleSort();
    //    System.out.println(list);
    verifySortedList(list);
  }

  @Test
  void quickSort() {
    var list = genList();
    list.quickSort();
    verifySortedList(list);
  }

  @Test
  void search() {
    var list = genList();
//    var randIndex = random.nextInt(SIZE);
//    var element = list.get(randIndex);
//    System.out.println("to search: " + element);
    list.quickSort();
    System.out.println(list);
//    var index = list.binarySearch(element);
    var index = list.binarySearch(50);
    System.out.println("search find :" + index);
  }
}
